﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Runtime.Serialization;
using DALEntities;



namespace MapEntities
{
    [Serializable]
    public class ShapeItemVisualization
    {
        private ShapeBackgroundVisualization backgroundVisialization = null;


        public ShapeItemVisualization()
        {

        }

        public ShapeBackgroundVisualization BackgroundVisialization
        {
            get
            {
                if (backgroundVisialization == null)
                    backgroundVisialization = new ShapeBackgroundVisualization();
                return backgroundVisialization;
            }

            set
            {
                backgroundVisialization = value;
            }
        }
    }

    [Serializable]
    public enum ShapeBackgroundVisualizationMode
    {
        Automatic,
        UserDefined
    }







    [Serializable]
    public class ShapeBackgroundAutomaticVisualization
    {
        private ShapeColoringInfo coloringInfo = null;

        public ShapeColoringInfo ColoringInfo
        {
            get
            {
                if (coloringInfo == null)
                {
                    coloringInfo = new ShapeColoringInfo();
                    return coloringInfo;
                }
                else return coloringInfo;
            }
            set
            {
                coloringInfo = value;
            }
        }
    }

    [Serializable]
    public class ShapeBackgroundRange : ItemRange
    {

        private ShapeColoringInfo coloring;
        public ShapeColoringInfo Coloring
        {
            get
            {
                if (coloring == null) coloring = new ShapeColoringInfo();
                return coloring;
            }
            set
            {
                coloring = value;
            }
        }

    }

    [Serializable]
    public class ShapeBackgroundRangeList
    {
        public ShapeBackgroundRange this[int index]
        {
            get
            {
                return List[index];
            }

            set
            {
                List[index] = value;
            }
        }

        public void Clear()
        {
            List.Clear();
        }

        public void Add(ShapeBackgroundRange item)
        {
            List.Add(item);
        }

        private ShapeColoringInfo defaulColoringInfo = null;
        private List<ShapeBackgroundRange> list = null;

        public List<ShapeBackgroundRange> List
        {
            get
            {
                if (list == null)
                    list = new List<ShapeBackgroundRange>();
                return list;
            }

            set
            {
                list = value;
            }
        }

        public void RemoveAt(int index)
        {
            List.RemoveAt(index);
        }

        public int Count
        {
            get
            {
                return List.Count;
            }
        }

        public ShapeColoringInfo DefaulColoringInfo
        {
            get
            {
                if (defaulColoringInfo == null)
                    defaulColoringInfo = new ShapeColoringInfo();
                return defaulColoringInfo;
            }
            set { defaulColoringInfo = value; }
        }

        internal DataType DataType { get; set; }

        public void ConvertRanges()
        {
            ShapeBackgroundRange xtemp = null; ;
            try
            {
                foreach (ShapeBackgroundRange xitem in this.List)
                {
                    xtemp = xitem;
                    switch (DataType)
                    {
                        case DataType.Integer:
                            {
                                xitem.Start = Convert.ToInt32(xitem.Start);
                                xitem.Finish = Convert.ToInt32(xitem.Finish);
                                break;
                            }
                        case DataType.DateTime:
                            {
                                xitem.Start = DateTime.Parse(xitem.Start.ToString().Trim());
                                xitem.Finish = DateTime.Parse(xitem.Finish.ToString().Trim());
                                break;
                            }
                        case DataType.Decimal:
                            {
                                xitem.Start = Convert.ToDecimal(xitem.Start);
                                xitem.Finish = Convert.ToDecimal(xitem.Finish);
                                break;
                            }
                        case DataType.Double:
                            {
                                xitem.Start = Convert.ToDouble(xitem.Start);
                                xitem.Finish = Convert.ToDouble(xitem.Finish);
                                break;
                            }
                        case DataType.Int64:
                            {
                                xitem.Start = Convert.ToInt64(xitem.Start);
                                xitem.Finish = Convert.ToInt64(xitem.Finish);
                                break;
                            }
                        case DataType.String:
                            {
                                xitem.Start = Convert.ToString(xitem.Start);
                                xitem.Finish = Convert.ToString(xitem.Finish);
                                break;
                            }
                    }
                }
            }
            catch
            {
                Exception ee = new Exception("Range \"" + xtemp.Start + " - " + xtemp.Finish + "\" can't be converted to " + DataType.ToString() + " type");
                throw ee;
            }
        }

        public bool InRange(object value, out ShapeBackgroundRange xitemrange)
        {
            bool xresult = false;
            xitemrange = null;
            if (value == null)
                return xresult;
            else
            {
                if (value is IComparable)
                {
                    try
                    {
                        foreach (ShapeBackgroundRange item in this.List)
                        {
                            int cRes1 = ((IComparable)value).CompareTo(item.Start);
                            int cRes2 = ((IComparable)value).CompareTo(item.Finish);
                            if (cRes1 >= 0 && cRes2 <= 0)
                            {
                                xresult = true;
                                xitemrange = item;
                                break;
                            }
                        }
                        return xresult;
                    }
                    catch
                    {
                        return xresult;
                    }


                }
                else return xresult;
            }
        }

        //#region IEnumerable<ShapeBackgroundRange> Members

        //public IEnumerator<ShapeBackgroundRange> GetEnumerator()
        //{
        //    return List.GetEnumerator();
        //}

        //#endregion

        //#region IEnumerable Members

        //System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        //{
        //    return List.GetEnumerator();
        //}

        //#endregion
    }

    [Serializable]
    public class ShapeBackgroundVisualization
    {
        public ShapeBackgroundVisualizationMode Mode { get; set; }
        private ShapeBackgroundAutomaticVisualization automaticVisualizationInfo = null;
        private ShapeBackgroundRangeList rangeList = null;
        private DataType dataType = DataType.Integer;

        public DataType DataType
        {
            get { return dataType; }
            set
            {
                dataType = value;
                if (rangeList != null)
                    rangeList.DataType = dataType;
            }
        }

        public ShapeBackgroundAutomaticVisualization Automatic
        {
            get
            {
                if (automaticVisualizationInfo == null)
                    automaticVisualizationInfo = new ShapeBackgroundAutomaticVisualization();
                return automaticVisualizationInfo;
            }
            set
            {
                automaticVisualizationInfo = value;
            }
        }

        public ShapeBackgroundRangeList RangeList
        {
            get
            {
                if (rangeList == null)
                {
                    rangeList = new ShapeBackgroundRangeList();
                    rangeList.DataType = this.DataType;
                }
                return rangeList;
            }

            set
            {
                rangeList = value;
            }
        }

        public ShapeBackgroundVisualization()
        {

        }

    }
}
